$(function(){
    showCondition(true, 0, 0, 1,[]); //加载查询条件
	//获取前台传来的参数
	var param = drgUI.setAjaxData(tabId);
	
    $.post(contextPath+"/medicalRecordQuality/initializeFullScore.dhc",{}).done(function(result){
        fullScore = result.data
    	//加载出院病例明细
    	loadMRQualityCaseDtlTable(null);
    })
})

var fullScore;//保存大类和分类的满分信息
//加载出院病例明细表
function loadMRQualityCaseDtlTable(searchCondtion){
    var columns=[
        {"data": "P00050000","title":"病案号",
            "render": function(obj, type, full){
                return "<a href=\"javascript:medicalRecord('"+full.pmainIndex+"','"+full.P00060000+"')\">"+full.P00050000+"</a>";
            }
        },
        {"data": "P00060000","title":"姓名" },
        {"data": "gender","title":"性别",
            "render": function(obj, type, full) {
                if(full.gender=="1") {
                    return "男";
                } else if(full.gender=="0"){
                    return "女" ;
                } else{
                    return full.gender;
                }
            }
        },
        {"data": "inDate","title":"入院时间"},
        {"data": "outDate","title":"出院时间"},
        {"data": "DEP_NAME","title":"出院科室"},
        {"data": "totalFee","title":"总费用",
	        "render":function ( tdData, type, rowData, meta ) {
		    	//tdData:当前cell的值（基于 columns.data）      rowData:整个row的数据（不基于 columns.data）
		    	//meta.row 行下标从0开始，meta.col 列下标从0开始
		    	//如果是过滤或者排序则返回原始数据
		    	if(type =='filter'||type =='sort'){
		    		return tdData;
		    	}
		    	return formatNumber(tdData ,2);
		    }
        },
        {"data": "ruleDetail","title":"违反规则",
            "render": function(obj, type, full) {
            	var showRule = '';
            	if(obj){            		
            		var rules = obj.split('@@');
            		if(rules && rules.length > 3){
            			showRule = '<div style="text-align: left;height:60px;overflow-y:scroll">';
            		}else{
            			showRule = '<div style="text-align: left;">';
            		}
            		$.each(rules,function(index,value){
            			if ($.trim(value)){
                			if (value.length > 40){
                				showRule += linefeedText(value,40);               				
                			}else{
                				showRule += value+'<br/>';
                			}            				
            			}          			
            		});
            		showRule += '</div>';
            	}          	
            	return showRule ;
            }
        },
        {"data": "totalScore","title":"总得分"},
        {"data": "ratingLevelDsc","title":"评定等级"}
    ];
    if(fullScore) {
        // fullScore = {totalScore: {DT: 50, COST: 6, BASICINFO: 18, IP: 26}, partScore: {BASICINFO@@D: 4,COST@@D: 2, IP@@B: 10, DT@@C: 3},
        //  partClass: {A: "A类", B: "B类", C: "C类", D: "D类"},wholeClass: {DT: "诊疗信息", COST: "费用信息", BASICINFO: "患者基本信息", IP: "住院过程信息"} };
    	var totalClass = fullScore.wholeClass ;
    	var totalScore = fullScore.totalScore ;
    	for(var o in totalClass){
        	var ob = {};
        	//因为取出来的totalClass-o 的格式是 DT_ZhongTie17，但是下面loadMRQualityCaseDtl.dhc取出来的数据都是大写DT_ZHONGTIE17
        	ob["data"] = o.toUpperCase();
        	ob["title"] = totalClass[o]+"("+totalScore[o]+"分)";
        	ob["render"] = function ( tdData, type, rowData, meta ) {
		    	//tdData:当前cell的值（基于 columns.data）      rowData:整个row的数据（不基于 columns.data）
		    	//meta.row 行下标从0开始，meta.col 列下标从0开始
		    	//如果是过滤或者排序则返回原始数据
		    	if(type =='filter'||type =='sort'){
		    		return tdData;
		    	}
		    	return formatNumber(tdData ,2);
		    }
        	columns.push(ob);
    	}
    }

    var ajaxFunction=function (aoData, fnCallback,oSetting ) {
        orderColumn=columns[aoData.order[0].column].data;//获取排序的字段
        orderMethod=aoData.order[0].dir;//获取asc desc
        var iDisplayStart=aoData.start;
        var iDisplayLength=aoData.length;
        var sEcho = 1;
        //获取病例总条数
        var caseTotalCount = parseInt($('#'+tabId+' #caseTotalCount').val());
        var param=drgUI.setAjaxData(tabId);
        param.dateType = "outDate" ;
        param.searchCondtion=searchCondtion;
        param.rowStart= iDisplayStart; //开始行数
        param.pageSize= iDisplayLength;
        param.sEcho=sEcho;
        param.caseTotalCount= caseTotalCount;
        param.orderColumn=orderColumn;
        param.orderMethod=orderMethod;
        $.ajax( {
            "dataType": 'json',
            "type": "POST",
            "url": contextPath + "/medicalRecordQuality/loadMRQualityCaseDtl.dhc",
            "data": param ,
            "success": function(resp) {
                var data=[];
                if(resp.success){
                    $('#'+tabId+' #caseTotalCount').val(resp.data.iTotalDisplayRecords);//总条数赋值到隐藏域
                }
                data.draw=aoData.draw;
                data.recordsTotal=resp.data.iTotalRecords;
                data.recordsFiltered=resp.data.iTotalDisplayRecords;
                data.data=resp.data.aaData;
                fnCallback(data,oSetting);
            }
        } );
    };
    var columnDefs = [{"orderable": true,"targets": [0,1,5] },{"orderable": false,"targets": '_all' }] ;
    var paramsDataTables={ajaxFunction:ajaxFunction,columnDefs:columnDefs,tableTitle:'病例明细表',scrollX:true,scrollY:'320px',  pageLength:50,lengthMenu:[[ 50,100,500, 1000,99999],[ 50,100,500, 1000,'全部']],fixedColumns:{'leftColumns' : 7}};
    var tb_id = "mrQualityCaseDtlTable" ;
    var oTable=$('#'+tabId+' #'+tb_id).dataTableServerPaging(columns,paramsDataTables);
    $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollBody table tbody").attr('id','table');//tbody加上ID，病案号的样式
    $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollHeadInner table thead th").css({'text-align':'center'});
    oTable.columns.adjust();//tbody加了样式，重新调整宽度
    
    if(searchCondtion){
        $("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室' placeholder='病案号、姓名、出院科室' value='"+searchCondtion+"'></label>")
    }else{
        $("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室' placeholder='病案号、姓名、出院科室'></label>")
    }

    //重写过滤方法
    $("#"+tabId+" #"+tb_id+"_filter_value").keydown(function(e){
        if(e.keyCode==13){
            var searchCondtion = $("#"+tabId+" #"+tb_id+"_filter_value").val();
            if(searchCondtion.length==0){
                $('#'+tabId+' #caseTotalCount').val(0);//总条数赋值到隐藏域
            }
            loadMRQualityCaseDtlTable(searchCondtion) ;
        }
    });
}
